package web.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class PropertiesUtil {
    //配置文件的路径
    private String configPath=null;
    
    /**
     * 配置文件对象
     */
    private Properties props=null;
    
    public PropertiesUtil() throws IOException{
//        InputStream in = new FileInputStream("config/config.properties");
        InputStream in=new FileInputStream("classpath:config.properties");
        props = new Properties();
        props.load(in);
        //关闭资源
        in.close();
    }
    
	public String readValue(String key) throws IOException {
        return  props.getProperty(key);
    }
    
	 public Map<String,String> readAllProperties() throws FileNotFoundException,IOException  {
	        //保存所有的键值
	        Map<String,String> map=new HashMap<String,String>();
	        Enumeration en = props.propertyNames();
	        while (en.hasMoreElements()) {
	            String key = (String) en.nextElement();
	            String Property = props.getProperty(key);
	            map.put(key, Property);
	        }
	        return map;
	    }
	 
	 public static void main(String[] args) {
	        PropertiesUtil p;
	        try {
	            p = new PropertiesUtil();
	            System.out.println(p.readValue("url"));
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }
}
